package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.dc.bobaoge.base.entity.message.common.po.MessageComplaint;
import com.dc.bobaoge.base.mapper.message.MessageComplaintMapper;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreMessageReport;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageReportMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * MessageComplaintResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-13 10:20:13
 */
@Slf4j
@Component
public class MessageComplaintResolveMethod implements ResolveMethod {

    @Resource
    private MessageComplaintMapper messageComplaintMapper;

    @Resource
    private OldCoreMessageReportMapper oldCoreMessageReportMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        var complaint = messageComplaintMapper.selectById(trigger.getTargetId());
        if (complaint == null) {
            throw new BusinessSimpleException("举报信息不存在: " + trigger.getTargetId());
        }
        // 查询旧数据库中是否有这条数据
        var report = oldCoreMessageReportMapper.selectById(trigger.getTargetId());
        if (report == null) {
            // 执行新增操作
            add(trigger, complaint);
            return;
        }
        report.setReportMsgId(complaint.getFkMessageId());
        report.setReportUserId(complaint.getFkUserId());
        report.setReportUserPhone(complaint.getConcatPhone());
        report.setReportContent(complaint.getContent());
        report.setReportTime(complaint.getGmtCreate());
        oldCoreMessageReportMapper.updateById(report);
    }

    /**
     * 新增的处理方法
     *
     * @param trigger   日志信息
     * @param complaint 新增的信息
     */
    private void add(SyncTrigger trigger, MessageComplaint complaint) {
        var report = new OldCoreMessageReport();
        report.setReportId(complaint.getId());
        report.setReportMsgId(complaint.getFkMessageId());
        report.setReportUserId(complaint.getFkUserId());
        report.setReportUserPhone(complaint.getConcatPhone());
        report.setReportContent(complaint.getContent());
        report.setReportTime(complaint.getGmtCreate());
        oldCoreMessageReportMapper.insert(report);
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "message_complaint";
    }

}
